package com.nanning.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanning.system.domain.TaskComplete;
import com.nanning.system.mapper.TaskCompleteMapper;
import com.nanning.system.service.ITaskCompleteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 任务完成情况 服务实现类
 * </p>
 *
 * @author zhurenwang
 * @since 2025-05-03
 */
@Slf4j
@Service
public class TaskCompleteServiceImpl extends ServiceImpl<TaskCompleteMapper, TaskComplete> implements ITaskCompleteService {

    private static final Map<String, String> map = new HashMap<>();

    static {
        map.put("发送吨-月累完成", "5,5");
        map.put("发送吨-月计划进度", "5,4");
        map.put("发送吨-月计划", "5,3");
        map.put("发送吨-年累完成", "5,11");
        map.put("发送吨-年计划进度", "5,10");
        map.put("发送吨-年计划", "5,9");

        map.put("运输收入-月累完成", "6,5");
        map.put("运输收入-月计划进度", "6,4");
        map.put("运输收入-月计划", "6,3");
        map.put("运输收入-年累完成", "6,11");
        map.put("运输收入-年计划进度", "6,10");
        map.put("运输收入-年计划", "6,9");

        map.put("集装箱-月累完成", "7,5");
        map.put("集装箱-月计划进度", "7,4");
        map.put("集装箱-月计划", "7,3");
        map.put("集装箱-年累完成", "7,11");
        map.put("集装箱-年计划进度", "7,10");
        map.put("集装箱-年计划", "7,9");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importData(Sheet sheet) {
        baseMapper.delete(new QueryWrapper<>());
        map.forEach((k, v) -> {
            TaskComplete taskComplete = new TaskComplete();
            String[] ks = k.split("-");
            taskComplete.setType(ks[0]);
            taskComplete.setTitle(ks[1]);
            String[] vs = v.split(",");
            Cell cell = sheet.getRow(Integer.parseInt(vs[0])).getCell(Integer.parseInt(vs[1]));
            if (cell.getCellType() == CellType.NUMERIC) {
                taskComplete.setNum(BigDecimal.valueOf(cell.getNumericCellValue()));
            } else if (cell.getCellType() == CellType.STRING) {
                taskComplete.setNum(new BigDecimal(cell.getStringCellValue()));
            } else {
                taskComplete.setNum(BigDecimal.ZERO);
            }
            save(taskComplete);
        });
    }
}
